package cn.zebra.dev.mall.module.brand.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.mall.module.brand.dto.BrandDTO;
import cn.zebra.dev.product.moudle.brand.entity.BrandEntity;
import cn.zebra.dev.product.moudle.brand.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "商品品牌")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/list")
    @ApiOperation("商品品牌列表")
    public Result<PageUtils<BrandDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page){
        Map<String, Object> params = new HashMap<>();
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<BrandEntity> pageUtils = brandService.findPage(params);
        List<BrandEntity> list = pageUtils.getList();
        PageUtils<BrandDTO> data = new PageUtils<>(pageUtils);
        if (list.size() > 0) {
            List<BrandDTO> dtoList = new ArrayList<>();
            list.forEach(entity -> {
                BrandDTO dto = new BrandDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }
}
